package erp.server.resource.service.impl;

import erp.server.base.exception.ServiceException;
import erp.server.base.response.StatusCode;
import erp.server.resource.mapper.ResourceMapper;
import erp.server.resource.pojo.dto.ResourceDTO;
import erp.server.resource.pojo.entity.Resource;
import erp.server.resource.pojo.vo.ResourceDetailVO;
import erp.server.resource.pojo.vo.ResourceIndexVO;
import erp.server.resource.service.ResourceService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
public class ResourceServiceImpl implements ResourceService {
    @Autowired
    ResourceMapper mapper;
    @Override
    public void addNew(ResourceDTO ResourceDTO) {
        Resource Resource = new Resource();
        BeanUtils.copyProperties(ResourceDTO,Resource);
        Resource.setCreated(new Date());
        int rows = mapper.insert(Resource);
        if(rows==0){
            throw new ServiceException(StatusCode.OPERATION_FAILED);
        }
    }

    @Override
    public List<ResourceIndexVO> listForIndex() {
        return mapper.selectForIndex();
    }

    @Override
    public List<ResourceIndexVO> listForIndexByOrgId(Long userId) {
        return mapper.selectForIndexByOrgId(userId);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteById(id);
    }

    @Override
    public ResourceDetailVO detailById(Long id) {
        return mapper.detailById(id);
    }
}
